package com.cloud.service.Impl;

import com.cloud.dao.ApprovalNoticeDao;
import com.cloud.domain.LoginUser;
import com.cloud.myInterface.Factory;
import com.cloud.pojo.ApprovalNoticeForApproval;
import com.cloud.pojo.ApprovalNoticeForApprovalVue;
import com.cloud.service.ApprovalNoticeService;
import com.cloud.pojo.ApprovalNotice;
import com.cloud.tools.CommonResult;
import com.cloud.tools.CommonResultFactory;
import com.cloud.utils.MyTime;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @author： linJianHong
 * @dateTime： 14:40 2022/4/24
 * @Description:
 * @ClassName:
 */
@Service
public class ApprovalNoticeServiceImpl implements ApprovalNoticeService {

    @Resource
    ApprovalNoticeDao approvalNoticeDao;

    private final Factory resultFactory = new CommonResultFactory();

    @Override
    public CommonResult create(Map<String, Object> map) {
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String approvalId = loginUser.getUser().getUserId();
        int regionId = loginUser.getUser().getDefaultRegion();
        String notice = (String)map.get("notice");
        String title = (String)map.get("title");
        String onlyForVolunteer = (String)map.get("onlyForVolunteer");

        ApprovalNotice approvalNotice = new ApprovalNotice();
        approvalNotice.setApprovalId(approvalId);
        approvalNotice.setTitle(title);
        approvalNotice.setNotice(notice);
        approvalNotice.setPublishTime(MyTime.getCurrTimeAsDate());
        approvalNotice.setRegionId(regionId);
        approvalNotice.setOnlyForVolunteer(onlyForVolunteer);

        int res = approvalNoticeDao.create(approvalNotice);
        if(res == 1){
            return resultFactory.getResultInstance(200,"公告创建成功",1);
        }
        return resultFactory.getResultInstance(200,"公告创建失败，请稍后重试",0);
    }

    @Override
    public CommonResult update(Map<String, Object> map) {
        int id = (int)map.get("id");
        String notice = (String)map.get("notice");
        String title = (String)map.get("title");
        String onlyForVolunteer = (String)map.get("onlyForVolunteer");

        int res = approvalNoticeDao.update(id, title, notice, MyTime.getCurrTimeAsDate(), onlyForVolunteer);
        if(res == 1){
            return resultFactory.getResultInstance(200,"公告修改成功",1);
        }
        return resultFactory.getResultInstance(200,"公告修改失败，请稍后重试",0);
    }

    @Override
    public CommonResult delete(Map<String, Object> map) {
        int id = (int)map.get("id");
        int res = approvalNoticeDao.delete(id);
        CommonResult<Integer> commonResult;
        if(res == 1){
            return resultFactory.getResultInstance(200,"公告删除成功",1);
        }
        return resultFactory.getResultInstance(200,"公告删除失败，请稍后重试",0);
    }

    @Override
    public CommonResult retrieve() {
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String approvalId = loginUser.getUser().getUserId();
        List<ApprovalNoticeForApproval> res1 = approvalNoticeDao.retrieve(approvalId);
        List<ApprovalNoticeForApprovalVue> res2 = new ArrayList<>();
        for(ApprovalNoticeForApproval item: res1){
            ApprovalNoticeForApprovalVue approvalNoticeForApprovalVue = new ApprovalNoticeForApprovalVue();
            approvalNoticeForApprovalVue.setId(item.getId());
            approvalNoticeForApprovalVue.setNotice(item.getNotice());
            approvalNoticeForApprovalVue.setOnlyForVolunteer(item.getOnlyForVolunteer());
            approvalNoticeForApprovalVue.setPublishTime(MyTime.dateTime2String(item.getPublishTime()));
            approvalNoticeForApprovalVue.setTitle(item.getTitle());
            res2.add(approvalNoticeForApprovalVue);
        }
        if(res2.size() >= 1){
            return resultFactory.getResultInstance(200,"公告查看成功",res2);
        }
        return resultFactory.getResultInstance(200,"暂无公告",null);
    }
}
